package org.sunshine.miracle.dao;

import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionTemplate;
import org.sunshine.miracle.SystemConfiguration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.sql.DataSource;

public class DaoUtils {

    private static final Log LOGGER = LogFactory.getLog(DaoUtils.class);


    private static DataSource defaultDataSource;

    static DataSource getDefaultDataSource(){
        if (defaultDataSource == null ){
            String className = SystemConfiguration.getValue("db.driverClassName");
            String url = SystemConfiguration.getValue("db.url");
            String username = SystemConfiguration.getValue("db.username");
            String password = SystemConfiguration.getValue("db.password");
            defaultDataSource = new DriverManagerDataSource(className, url, username, password);
        }
        return defaultDataSource;
    }

    private static PlatformTransactionManager transactionManager;

    static PlatformTransactionManager getTransactionManager(){
        if (transactionManager == null) {
            transactionManager = new DataSourceTransactionManager(getDefaultDataSource());
        }
        return transactionManager;
    }

    private static TransactionTemplate transactionTemplate;

    static TransactionTemplate getTransactionTemplate(){
        if(transactionTemplate == null) {
            transactionTemplate = new TransactionTemplate(getTransactionManager());
        }
        return transactionTemplate;
    }

    private static TransactionStatus localTransactionStatus;

    static void startLocalTransaction() {
        if (localTransactionStatus != null) throw new IllegalStateException("Local transaction already running.");
        localTransactionStatus = getTransactionManager().getTransaction(getTransactionTemplate());
        LOGGER.debug("Local transation started.");
    }

    static void rollbackLocalTransaction(){
        if (localTransactionStatus == null) throw new IllegalStateException("Local transaction is not running.");
        getTransactionManager().rollback(localTransactionStatus);
        localTransactionStatus = null;
        LOGGER.debug("Local transation rolled back.");
    }

    static void commitLocalTransaction(){
        if (localTransactionStatus == null) throw new IllegalStateException("Local transaction is not running.");
        getTransactionManager().commit(localTransactionStatus);
        localTransactionStatus = null;
        LOGGER.debug("Local transation commited.");
    }

    private static FilesManager filesManager;

    public static FilesManager getFilesManager(){
        if (filesManager == null){
            filesManager = new FilesManager();
            filesManager.setDataSource(getDefaultDataSource());
        }
        return filesManager;
    }

}
